// catch的错误可能存在上传的不是图片等问题
const IMG_SIZE_ERROR = 'img_size_error'
// const IMG_TYPE_ENUMS = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif']
// typeMap = {
//   image: ['gif', 'jpg', 'jpeg', 'png', 'bmp', 'webp'],
//   video: ['mp4', 'm3u8', 'rmvb', 'avi', 'swf', '3gp', 'mkv', 'flv'],
//   audio: ['mp3', 'wav', 'wma', 'ogg', 'aac', 'flac'],
//   document: ['doc', 'txt', 'docx', 'pages', 'epub', 'pdf', 'numbers', 'csv', 'xls', 'xlsx', 'keynote', 'ppt', 'pptx']
// }
// Object.keys(typeMap).forEach(_type => {
//   const extensions = typeMap[_type]
//   if (extensions.indexOf(extension) > -1) {
//     type = _type
//   }
// })

import { handleTransformSize } from '@/utils/index'

const createImgUrl = file => {
  return (window.URL || window.webkitURL).createObjectURL(file)
}
export default {
  props: {
    // 上传图片宽度限制
    uploadWidth: {
      type: Number,
      default: undefined
    },
    // 上传图片高度限制
    uploadHeight: {
      type: Number,
      default: undefined
    },
    // 上传图片类型限制
    uploadType: {
      type: Array,
      default() {
        return []
      }
    },
    // 上传图片大小限制 - 单位(KB)
    size: {
      type: [Number, Boolean],
      default: 1024
    },
    // 自定义校验事件
    customValidate: {
      type: [Function, Boolean],
      default: false
    },
    // 上传图片tips
    uploadTips: {
      type: String,
      default: ''
    },
    // 自定义类
    customClass: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      currentWidth: undefined,
      currentHeight: undefined,
      currentFile: undefined,
      currentSize: undefined
    }
  },
  methods: {
    handleTransformSize,
    createImgUrl,
    /**
     * 类型校验
     */
    typeLimit(file) {
      const imgType = file.type.split('/')
      const rest = this.uploadType.length
        ? this.uploadType.includes(imgType[1])
        : true
      return rest ? '' : `上传图片应为${this.uploadType.join('、')}类型!`
    },
    /**
     * 图片大小校验
     */
    sizeLimit(file) {
      if (!this.size || file.size / 1024 <= this.size) {
        return ''
      }
      return `上传图片大小不能超过 ${this.handleTransformSize(this.size)}!`
    },
    /**
     * 自定义校验
     */
    customLimit(file) {
      const rest = this.customValidate
        ? this.customValidate({
          type: file.type,
          size: file.size,
          width: this.currentWidth,
          height: this.currentHeight
        })
        : ''
      return rest
    },
    /**
     * 图片宽高校验
     */
    widthHeightLimit(file) {
      if (!this.customValidate && (!this.uploadWidth || !this.uploadHeight)) {
        return ''
      }
      return new Promise((resolve, reject) => {
        const img = new Image()
        img.onload = () => {
          this.currentWidth = img.width
          this.currentHeight = img.height
          const valid =
            !this.uploadWidth || !this.uploadHeight
              ? true
              : img.width === this.uploadWidth &&
                img.height === this.uploadHeight
          valid ? resolve() : reject(IMG_SIZE_ERROR)
        }
        img.src = this.createImgUrl(file)
      }).then(
        () => {
          return ''
        },
        err => {
          if (err === IMG_SIZE_ERROR) {
            return `图片尺寸限制为${this.uploadWidth} x ${this.uploadHeight}!`
          }
        }
      )
    }
  }
}
